package com.des1gn.ymgetil.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.des1gn.ymgetil.service.ProxyService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Credentials;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProxyServiceImpl implements ProxyService, InitializingBean {

    private static final List<Proxy> proxys = new CopyOnWriteArrayList<>();


    @Override
    public List<String> createProxys() {
        List<Proxy> thisProxy = new LinkedList<>();
        String json = HttpUtil.get("http://tiqu.pyhttp.taolop.com/getip?count=10&neek=16356&type=2&yys=0&port=2&sb=&mr=3&sep=0&ts=1&city=440100&time=3");
        if (!StringUtils.isEmpty(json)){
            JSONObject jsonObject = JSONObject.parseObject(json);
            JSONArray data = jsonObject.getJSONArray("data");
            for (Object obj : data){
                JSONObject jsonData = JSONObject.parseObject(JSONObject.toJSONString(obj));
                if (voliateProxy(jsonData.getString("ip"),jsonData.getInteger("port"))) {
                    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(jsonData.getString("ip"), jsonData.getInteger("port")));
                    thisProxy.add(proxy);
                }
            }
        }
        proxys.clear();
        proxys.addAll(thisProxy);
        log.warn("当前代理池:{}",JSONObject.toJSONString(proxys));
        List<String> result = proxys.stream().map(i -> i.toString()).collect(Collectors.toList());
        return result;
    }
    
    private Boolean voliateProxy(String host,Integer port){
        String body = null;
        try {
            String des1gn = Credentials.basic("des1gn", "1132099462ad123");
            HttpRequest get = HttpUtil.createGet("https://www.baidu.com/");
            get.setHttpProxy(host,port);
            get.header("Proxy-Authorization",des1gn);
            get.setConnectionTimeout(5000);
            get.setReadTimeout(5000);
            body = get.execute().body();
        }catch (Exception e){
        }
        if (!org.thymeleaf.util.StringUtils.isEmpty(body) && body.indexOf("百度") > -1){
            return true;
        }
        return false;

    }

    @Override
    public Proxy getRandomProxy() {
        if (CollectionUtils.isEmpty(proxys)){
            return null;
        }
        int index = RandomUtil.randomInt(proxys.size());
        return proxys.get(index);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    }
}
